package com.hooper.executor.demo;

import java.util.concurrent.*;

public class FutureExample {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executorService = Executors.newFixedThreadPool(2); //创建线程池
        Future<Integer> future1 = executorService.submit(() -> {
            Thread.sleep(5000); //模拟第一个远程接口的计算
            return 1;
        });
        Future<Integer> future2 = executorService.submit(() -> {
            Thread.sleep(3000); //模拟第二个远程接口的计算
            return 2;
        });
        int result = future1.get() + future2.get(); //获取两个Future对象的结果并相加
        System.out.println("计算结果为：" + result); //输出结果
        executorService.shutdown(); //关闭线程池

    }
}
